import { ref, computed } from "vue";
import { LogService, type TextContent, type GeneratedImage } from "@/api/log";

/**
 * 日志相关的组合式函数
 */
export function useLog() {
  // 响应式状态
  const textHistory = ref<TextContent[]>([]);
  const imageHistory = ref<GeneratedImage[]>([]);
  const isLoading = ref(false);
  const error = ref<string | null>(null);
  const searchKeyword = ref("");

  // 计算属性
  const filteredTextHistory = computed(() => {
    return LogService.filterRecords(textHistory.value, searchKeyword.value);
  });

  const filteredImageHistory = computed(() => {
    return LogService.filterRecords(imageHistory.value, searchKeyword.value);
  });

  const sortedTextHistory = computed(() => {
    return LogService.sortByTime(filteredTextHistory.value, "desc");
  });

  const sortedImageHistory = computed(() => {
    return LogService.sortByTime(filteredImageHistory.value, "desc");
  });

  const totalRecords = computed(() => {
    return textHistory.value.length + imageHistory.value.length;
  });

  /**
   * 加载用户的历史记录
   */
  const loadHistory = async (userId: number) => {
    try {
      isLoading.value = true;
      error.value = null;

      const result = await LogService.getAllHistory(userId);
      textHistory.value = result.textHistory;
      imageHistory.value = result.imageHistory;
    } catch (err) {
      error.value = err instanceof Error ? err.message : "加载历史记录失败";
      console.error("加载历史记录失败:", err);
    } finally {
      isLoading.value = false;
    }
  };

  /**
   * 加载文案历史记录
   */
  const loadTextHistory = async (userId: number) => {
    try {
      isLoading.value = true;
      error.value = null;

      const data = await LogService.getTextHistory({ userId });
      textHistory.value = data;
    } catch (err) {
      error.value = err instanceof Error ? err.message : "加载文案历史记录失败";
      console.error("加载文案历史记录失败:", err);
    } finally {
      isLoading.value = false;
    }
  };

  /**
   * 加载图片历史记录
   */
  const loadImageHistory = async (userId: number) => {
    try {
      isLoading.value = true;
      error.value = null;

      const data = await LogService.getImageHistory({ userId });
      imageHistory.value = data;
    } catch (err) {
      error.value = err instanceof Error ? err.message : "加载图片历史记录失败";
      console.error("加载图片历史记录失败:", err);
    } finally {
      isLoading.value = false;
    }
  };

  /**
   * 设置搜索关键词
   */
  const setSearchKeyword = (keyword: string) => {
    searchKeyword.value = keyword;
  };

  /**
   * 清除搜索
   */
  const clearSearch = () => {
    searchKeyword.value = "";
  };

  /**
   * 清除错误信息
   */
  const clearError = () => {
    error.value = null;
  };

  /**
   * 重置状态
   */
  const reset = () => {
    textHistory.value = [];
    imageHistory.value = [];
    isLoading.value = false;
    error.value = null;
    searchKeyword.value = "";
  };

  return {
    // 状态
    textHistory,
    imageHistory,
    isLoading,
    error,
    searchKeyword,

    // 计算属性
    filteredTextHistory,
    filteredImageHistory,
    sortedTextHistory,
    sortedImageHistory,
    totalRecords,

    // 方法
    loadHistory,
    loadTextHistory,
    loadImageHistory,
    setSearchKeyword,
    clearSearch,
    clearError,
    reset,
  };
}
